package com.example;

import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import java.io.IOException;

// 学生界面控制器
public class StudentController {

    @FXML
    private Button backButton;
    @FXML
    private Button viewGradesButton;
    @FXML
    private Button viewScheduleButton;
    @FXML
    private Button viewAttendanceButton;
    @FXML
    private AnchorPane contentArea;

    private Stage primaryStage;
    private User currentUser;

    public void setPrimaryStage(Stage primaryStage) {
        this.primaryStage = primaryStage;
    }

    public void setCurrentUser(User user) {
        this.currentUser = user;
        System.out.println("StudentController received user: " + (user != null ? user.getUsername() : "null"));
    }

    // 处理"查看 / 修改基本信息"按钮点击
    @FXML
    private void handlePersonalInformation() {
        loadSubScene("StudentPersonalInformation.fxml", "个人信息管理");
    }

    // 处理"查看个人学籍信息"按钮点击
    @FXML
    private void handleAcademicInformation() {
        loadSubScene("StudentAcademicInformation.fxml", "个人学籍信息");
    }

    // 处理"查看所有课程成绩"按钮点击
    @FXML
    private void handleViewGrades() {
        loadSubScene("StudentViewGrades.fxml", "查看成绩");
    }

    // 处理"成绩趋势分析"按钮点击
    @FXML
    private void handleGradeAnalysis() {
        loadSubScene(
            "StudentGradeAnalysis.fxml",
            "成绩趋势分析"
        );
    }

    // 处理"查看个人课表"按钮点击
    @FXML
    private void handleViewSchedule() {
        loadSubScene("StudentSchedule.fxml", "查看课表");
    }

    // 处理"查看已选课程及学分"按钮点击
    @FXML
    private void handleViewSelectedCourses() {
        loadSubScene(
            "StudentSelectedCourses.fxml",
            "查看已选课程及学分"
        );
    }

    // 处理"下载课程大纲"按钮点击
    @FXML
    private void handleDownloadSyllabus() {
        loadSubScene("StudentDownloadSyllabus.fxml", "下载课程大纲");
    }

    // 处理"接收成绩发布通知"按钮点击
    @FXML
    private void handleViewNotifications() {
        loadSubScene("StudentViewNotifications.fxml", "接收成绩发布通知");
    }

    // 处理"查看班级 / 学校公告"按钮点击
    @FXML
    private void handleViewAnnouncements() {
        loadSubScene("StudentViewAnnouncements.fxml", "查看班级 / 学校公告");
    }

    @FXML
    private void handleBackButton() {
        try {
            // 加载登录界面
            FXMLLoader loader = new FXMLLoader(getClass().getResource("Login.fxml"));
            Parent root = loader.load();
            
            // 获取 LoginController 并设置 Stage
            LoginController loginController = loader.getController();
            Stage stage = (Stage) backButton.getScene().getWindow();
            loginController.setPrimaryStage(stage);
            
            // 设置新的场景
            Scene scene = new Scene(root);
            stage.setTitle("登录");
            stage.setScene(scene);
            stage.show();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @FXML
    private void handleViewAttendance() {
        loadSubScene("StudentViewAttendance.fxml", "查看考勤");
    }

    // 加载子场景的通用方法
    private void loadSubScene(String fxmlFile, String title) {
        try {
            FXMLLoader loader = new FXMLLoader(getClass().getResource(fxmlFile));
            Parent root = loader.load();

            // 如果子界面实现了StudentSubController接口，设置primaryStage和studentController
            if (loader.getController() instanceof StudentSubController) {
                StudentSubController subController = (StudentSubController) loader.getController();
                subController.setPrimaryStage(primaryStage);
                subController.setStudentController(this);

                // 如果子控制器需要当前用户，传递用户对象
                if (subController instanceof StudentPersonalInformationController) {
                    ((StudentPersonalInformationController) subController).setCurrentUser(currentUser);
                } else if (subController instanceof StudentAcademicInformationController) {
                    ((StudentAcademicInformationController) subController).setCurrentUser(currentUser);
                } else if (subController instanceof StudentViewGradesController) {
                     ((StudentViewGradesController) subController).setCurrentUser(currentUser);
                } else if (subController instanceof StudentScheduleController) {
                    ((StudentScheduleController) subController).setCurrentUser(currentUser);
                } else if (subController instanceof StudentSelectedCoursesController) {
                    ((StudentSelectedCoursesController) subController).setCurrentUser(currentUser);
                }
                // TODO: 为其他需要User的子控制器添加else if
            }

            contentArea.getChildren().clear();
            contentArea.getChildren().add(root);

            if (primaryStage != null) {
                primaryStage.setTitle("学生界面 - " + title);
            }
        } catch (Exception e) {
            e.printStackTrace();
            contentArea.getChildren().clear();
            contentArea.getChildren().add(new javafx.scene.control.Label(title + "功能正在开发中..."));
        }
    }

    // 新增方法：显示学生主界面的默认视图
    public void showStudentHomeView() {
        contentArea.getChildren().clear();
        // 可以在这里添加一些默认内容，比如欢迎标签
        contentArea.getChildren().add(new javafx.scene.control.Label("欢迎来到学生主界面！"));
        if (primaryStage != null) {
            primaryStage.setTitle("学生界面");
        }
    }

    // 新增方法：显示学生成绩列表视图
    public void showStudentViewGradesView() {
         loadSubScene("StudentViewGrades.fxml", "查看成绩");
    }

     // 新增方法：显示学生成绩详情视图
    public void showStudentGradeDetailsView(Grade grade) {
         try {
             FXMLLoader loader = new FXMLLoader(getClass().getResource("StudentGradeDetails.fxml"));
             Parent root = loader.load();

             StudentGradeDetailsController controller = loader.getController();
             controller.setPrimaryStage(primaryStage);
             controller.setStudentController(this);
             controller.setGrade(grade); // 传递选中的成绩对象

             contentArea.getChildren().clear();
             contentArea.getChildren().add(root);

             if (primaryStage != null) {
                 primaryStage.setTitle("学生界面 - 成绩详情");
             }
         } catch (IOException e) {
             e.printStackTrace();
             contentArea.getChildren().clear();
             contentArea.getChildren().add(new javafx.scene.control.Label("加载成绩详情失败。"));
         }
    }
} 